﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using dbPoll_Application.Models;

namespace dbPoll_Application.ViewModels
{
    public class ReportViewModel
    {
        public int questionID { get; set; }
        public IDictionary<String, int> responses { get; set; }
        public String text { get; set; }
        public String chartType { get; set; }
        public String valueType { get; set; }
        public String chartColours { get; set; }
        public IDictionary<String, int> demoResponses { get; set; }
        public IDictionary<String, int> compareResponses { get; set; }
        public IList<String> correct { get; set; }

        public ReportViewModel(int questionID, IDictionary<String, int> responses, IList<String> correct)
        {
            this.questionID = questionID;
            this.responses = responses;

            Question question = QuestionRepository.GetQuestionByID<Question>(questionID);
            if (question.correctAnswerIndicator.active)
            {
                this.correct = correct;
            }
            else
            {
                this.correct = null;
            }
            this.text = question.question;

            QuestionChart chart = question.resultsChart;
            this.chartType = chart.GetQuestionChartType();

            this.valueType = chart.valueType;

            String[] coloursArray = chart.colours.Split(new char[] {',', ' '});

            String chartColours = "";
            int i = 0;
            foreach (String colour in coloursArray) {
                if (colour != "")
                {
                    chartColours += "'" + colour + "'";
                    if (i < coloursArray.Length - 1)
                    {
                        chartColours += ", ";
                    }
                    i++;
                }
            }
            this.chartColours = chartColours;
        }

        public ReportViewModel(int questionID, IDictionary<String, int> demoResponses, IDictionary<String, int> compareResponses)
        {
            this.questionID = questionID;

            this.demoResponses = demoResponses;
            this.compareResponses = compareResponses;

            Question question = QuestionRepository.GetQuestionByID<Question>(questionID);
            this.text = question.question;

            QuestionChart chart = question.comparisonChart;
            this.chartType = chart.GetQuestionChartType();

            this.valueType = chart.valueType;

            String[] coloursArray = chart.colours.Split(new char[] { ',', ' ' });

            String chartColours = "";
            int i = 0;
            foreach (String colour in coloursArray)
            {
                if (colour != "")
                {
                    chartColours += "'" + colour + "'";
                    if (i < coloursArray.Length - 1)
                    {
                        chartColours += ", ";
                    }
                    i++;
                }
            }
            this.chartColours = chartColours;

        }
    }
}